package com.hzit.manager.service.impl;

import com.hzit.common.utils.R;
import com.hzit.manager.client.OrderClient;
import com.hzit.manager.excep.MyException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.common.utils.PageUtils;
import com.hzit.common.utils.Query;

import com.hzit.manager.dao.BrandDao;
import com.hzit.manager.entity.BrandEntity;
import com.hzit.manager.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private OrderClient orderClient;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );

        return new PageUtils(page);
    }

    //1. 开启全局事务 (TM TC RM)
    @GlobalTransactional
    public void saveBrandAndOrder() {
        //1.1 保存品牌
        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setFirstChar("Q");
        brandEntity.setName("七匹狼");
        this.save(brandEntity);

        //1.2 保存订单
        R r = orderClient.saveOrder();
        String code = r.get("code").toString();
        if(code.equals("500")){
            throw new MyException(code,"出现了服务调用异常！");
        }
    }
}